#include "Game.h"

CGame::CGame(HINSTANCE hInstance)
{
	GameWindows = new CWindows(hInstance);
	GameWindows->initializeWindows();

	CSprite::initializeSpriteHandler(GameWindows->getSpriteHandler());
	CSound::initializeWindowHandler(GameWindows->getWindowHandler());

	InputDevice_ = new CInput(hInstance,GameWindows->getWindowHandler());
	mapGame_.readMap(L"Map 1");
	contra_ = new CPlayer();
	text_ = new CText(GameWindows->getDevice());
	textPosition_.top = textPosition_.left = 200;
	textPosition_.right = textPosition_.bottom = 300;
	CCanon_White::setText(text_);
//	camera_ = new CCamera();
}


CGame::~CGame(void)
{
}

void CGame::Run()
{
	MSG msg;
	int done = 0;
	DWORD frame_start = GetTickCount();
	DWORD tick_per_frame = 1000 / SEC_PER_FRAME;
	while (!done) 
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if (msg.message==WM_QUIT) done=1;

			TranslateMessage(&msg);
			DispatchMessage(&msg);			
		}
		DWORD now = GetTickCount();
		DeltaTime_ = (now - frame_start); 
		if (DeltaTime_ >= tick_per_frame) 
		{
			frame_start = now;
			Update();
			Render();
		}
	}	
}

void CGame::Update()
{
   	InputDevice_->Update();
//	camera_->Update(InputDevice_, contra_->getPosition());
	contra_->UpdateKeyBoard(InputDevice_);
	contra_->Update(DeltaTime_);
	mapGame_.UpdateAI(contra_->getPosition());
	mapGame_.Update(DeltaTime_);
	if (InputDevice_->KeyDown(DIK_ESCAPE))
		PostQuitMessage(0);

}

void CGame::Render() 
{
	if (this->GameWindows->startDraw()) {
		mapGame_.Render();
		contra_->Render();
		text_->Draw(text_->tostrW(contra_->getPosition().x), textPosition_, DT_CENTER, D3DCOLOR_XRGB(255, 255, 255));
		//text_->Draw(CConvert::tostrW((CCanon_White*)mapGame_.getCanon())->getStatus(), textPosition_, DT_CENTER, D3DCOLOR_XRGB(255, 255, 255));
	}
	this->GameWindows->stopDraw();
}